
$(function(){
    // 从远程加载一个页面的模板显示在DOM中
    $("#titleDiv").load("js/titleHtmlTemplate.html","",function(){

        // 显示用户的登录状态
        $.ajax({
            url: "http://localhost/shop-sso/sso/getUserByUsername",
            type:"GET",
            dataType:"JSON",
            success:function(data){
                if(data.status == "success"){
                    var username = data.data.username;
                    $("#showLoginUserMsg").html("欢迎【"+username+"】登录,<a href='javascript:logout()'>注销</a>");
                }else{
                    $("#showLoginUserMsg").html("您还没有登录点击,<a href='http://localhost/shop-sso/toLoginPage'>这里登录</a>");
                }
            },
            beforeSend:function(XMLHttpRequest){ // 在发送请求之前调用
                var token = localStorage.getItem("2008-login-token");
                if(token){
                    XMLHttpRequest.setRequestHeader("token",token)
                }
            }
        });

    });
})

function logout(){
    localStorage.removeItem("2008-login-token");
    location.reload();
}